package com.taptech.doufu.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.view.theme.editnovel.EditNovelTheme;

/**
 * Created by Wyatt on 2017/11/28/028.
 */

public class EditNovelThemeAdapter extends RecyclerView.Adapter<EditNovelThemeAdapter.ViewHolder>{

    private SparseArray<Integer> array;
    private EditNovelTheme editNovelTheme;
    private OnClickItem onClickItem;

    public interface OnClickItem{
        void onClickItem(Integer themeType);
    }

    public EditNovelThemeAdapter(Context context, EditNovelTheme editNovelTheme, OnClickItem onClickItem){
        this.editNovelTheme = editNovelTheme;
        this.array = editNovelTheme.getMenuTheme().getThemeSelectArray();
        this.onClickItem = onClickItem;
    }

    public void updata(EditNovelTheme editNovelTheme) {
        this.editNovelTheme = editNovelTheme;
        notifyDataSetChanged();
    }


    private Integer getItem(int position) {
        if (array != null && array.size() > position) {
            return array.valueAt(position);
        }
        return 0;
    }

    private Integer getKey(int position) {
        if (array != null && array.size() > position) {
            return array.keyAt(position);
        }
        return 0;
    }

    private boolean isSelect(int position){
        if (array != null && array.size() > position){
            if (array.keyAt(position) == editNovelTheme.getThemeType()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_read_theme_select,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.ivTheme.setImageResource(getItem(position));
        holder.ivTheme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickItem.onClickItem(getKey(position));
            }
        });
        if (isSelect(position)) {
            holder.ivSelect.setVisibility(View.VISIBLE);
            holder.ivSelect.setImageResource(editNovelTheme.getMenuTheme().getThemeSelectIcon());
        }else{
            holder.ivSelect.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        if(array!=null){
            return array.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView ivTheme;
        public ImageView ivSelect;

        public ViewHolder(View itemView) {
            super(itemView);
            ivTheme = (ImageView) itemView.findViewById(R.id.ivTheme);
            ivSelect = (ImageView) itemView.findViewById(R.id.ivSelect);
        }

    }
}
